package com.xmtlxx.web.controller.system;

import cn.hutool.core.bean.BeanUtil;
import com.xmtlxx.common.annotation.Log;
import com.xmtlxx.common.annotation.RepeatSubmit;
import com.xmtlxx.common.core.controller.BaseController;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.domain.R;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.core.validate.AddGroup;
import com.xmtlxx.common.core.validate.EditGroup;
import com.xmtlxx.common.core.validate.QueryGroup;
import com.xmtlxx.common.enums.BusinessType;
import com.xmtlxx.common.utils.poi.ExcelUtil;
import com.xmtlxx.system.domain.bo.NoticeUserBo;
import com.xmtlxx.system.domain.vo.NoticeUserVo;
import com.xmtlxx.system.service.INoticeUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 公告用户Controller
 *
 * @author xmtlxx
 * @date 2022-05-22
 */
@Validated
@Api(value = "公告用户控制器", tags = {"公告用户管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/noticeUser")
public class SysNoticeUserController extends BaseController {

    private final INoticeUserService iNoticeUserService;

    /**
     * 返回一个初始化的对象到前端，方便进行动态渲染
     */
    @ApiOperation("用于返回一个初始化对象")
    @PostMapping("/initBean")
    R<NoticeUserVo> initBean(){
        NoticeUserVo vo = new NoticeUserVo();
        //可以对数据进行初始化
        return R.ok(vo);
    }

    /**
     * 查询公告用户列表
     */
    @ApiOperation("查询公告用户列表")
//    @SaCheckPermission("system:noticeUser:list")
    @GetMapping("/list")
    public TableDataInfo<NoticeUserVo> list(@Validated(QueryGroup.class) NoticeUserVo vo, PageQuery pageQuery) {
        return iNoticeUserService.queryPageList(vo,pageQuery);
    }

    /**
     * 导出公告用户列表
     */
    @ApiOperation("导出公告用户列表")
//    @SaCheckPermission("system:noticeUser:export")
    @Log(title = "公告用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated NoticeUserBo bo, HttpServletResponse response) {
        List<NoticeUserVo> list = iNoticeUserService.queryList(bo);
        ExcelUtil.exportExcel(list, "公告用户", NoticeUserVo.class, response);
    }

    /**
     * 获取公告用户详细信息
     */
    @ApiOperation("获取公告用户详细信息")
//    @SaCheckPermission("system:noticeUser:query")
    @GetMapping("/{noticeUserId}")
    public R<NoticeUserVo> getInfo(@ApiParam("主键")
                                     @NotNull(message = "主键不能为空")
                                     @PathVariable("noticeUserId") Integer noticeUserId) {
        return R.ok(iNoticeUserService.queryById(noticeUserId));
    }

    /**
     * 新增公告用户
     */
    @ApiOperation("新增公告用户")
//    @SaCheckPermission("system:noticeUser:add")
    @Log(title = "公告用户", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody NoticeUserVo vo) {
        NoticeUserBo bo = new NoticeUserBo();
        BeanUtil.copyProperties(vo,bo);
        return toAjax(iNoticeUserService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改公告用户
     */
    @ApiOperation("修改公告用户")
//    @SaCheckPermission("system:noticeUser:edit")
    @Log(title = "公告用户", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody NoticeUserVo vo) {
        NoticeUserBo bo = new NoticeUserBo();
        BeanUtil.copyProperties(vo,bo);
        return toAjax(iNoticeUserService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 公告删除
     */
    @ApiOperation("公告删除")
//    @SaCheckPermission("system:noticeUser:remove")
    @Log(title = "公告删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{noticeUserIds}")
    public R<Void> remove(@ApiParam("主键串")
                          @NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] noticeUserIds) {
        return toAjax(iNoticeUserService.deleteWithValidByIds(Arrays.asList(noticeUserIds), true) ? 1 : 0);
    }

    /**
     * 公告删除
     */
    @ApiOperation("公告阅读")
//    @SaCheckPermission("system:noticeUser:remove")
    @Log(title = "公告阅读", businessType = BusinessType.DELETE)
    @GetMapping("/read/{noticeUserIds}")
    public R<Void> read(@ApiParam("主键串")
                          @NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] noticeUserIds) {
        return toAjax(iNoticeUserService.deleteWithValidByIds(Arrays.asList(noticeUserIds), false) ? 1 : 0);
    }
}
